﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Cities;
using mojoPortal.Web;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Web;
using System.Globalization;

namespace blackhouse.Aparts.Web {
    public class ApartsSiteMapProvider : mojoPortalExtend.ExpandedSiteMapProvider {

        private Factory factory = Factory.Instance();
        private SiteCity city;

        protected override void CreateExpandedChildNodes(SiteMapNode parentNode) {
            if (parentNode.Url.EndsWith("/apartments"))
                this.MakeApartments(parentNode);
        }

        private void MakeApartments(SiteMapNode apartmentNode) {
            this.city = this.factory.GetSiteCity();
            if (apartmentNode.Title.Contains("{0}"))
                apartmentNode.Title = string.Format(apartmentNode.Title, this.city.SentenceName.ToFirstUpperString());

            apartmentNode.Url = blackhouse.mojoPortalExtend.PageProvider.AdjustUrlToCurrentSite("~/" + HttpContext.GetGlobalResourceObject("Aparts", "apartmentsUrl") + '-' + city.UrlName);

            List<mojoSiteMapNode> subNodes = this.GetApartmentNodes(apartmentNode);
            this.AddExpandedNodes(subNodes.ToArray());
        }

        private List<mojoSiteMapNode> GetApartmentNodes(SiteMapNode apartmentNode) {
            IApartmentGroup[] groups = this.factory.GetApartmentGroup().GetAllApartmentTypes();
            List<mojoSiteMapNode> nodes = new List<mojoSiteMapNode>(groups.Length);
            foreach (IApartmentGroup group in groups.OrderByDescending(d => d.Stars)) {
                nodes.Add(this.CreateNode(group, apartmentNode));
            }
            return nodes;
        }


        private mojoSiteMapNode CreateNode(IApartmentGroup group, SiteMapNode parentNode) {

            mojoSiteMapNode mojoParentNode = parentNode as mojoSiteMapNode;

            mojoSiteMapNode node = new mojoSiteMapNode(
                this,
                "apartment_" + group.Id,
                UrlUtils.GetApartmentTypeUrl(group, DisplayApartmentTypeView.Default, 1, SearchState.Empty),
                HttpContext.Current.Server.HtmlEncode(group.Name),
                group.GetSeo().PageDescription,
                parentNode.Roles,
                null,
                null,
                null);

            node.PageGuid = Guid.Empty;
            node.PageId = 10000 + group.Id;
            if (mojoParentNode != null) {
                node.ParentId = mojoParentNode.PageId;
                node.Depth = mojoParentNode.Depth + 1;
                node.ViewRoles = mojoParentNode.ViewRoles;
                node.EditRoles = mojoParentNode.EditRoles;
                node.CreateChildPageRoles = mojoParentNode.CreateChildPageRoles;
                node.CreateChildDraftPageRoles = mojoParentNode.CreateChildDraftPageRoles;
                node.IncludeInSiteMap = mojoParentNode.IncludeInSiteMap;
                node.IncludeInChildSiteMap = mojoParentNode.IncludeInChildSiteMap;
                node.IncludeInSearchMap = mojoParentNode.IncludeInSearchMap;
                node.LastModifiedUtc = group.LastMod;
                node.ChangeFrequency = mojoParentNode.ChangeFrequency;
                double siteMapPio = 0;
                if (Double.TryParse(mojoParentNode.SiteMapPriority, System.Globalization.NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out siteMapPio)) {
                    siteMapPio = siteMapPio - siteMapPio * 0.1;
                    siteMapPio = Math.Round(siteMapPio, 2);
                    node.SiteMapPriority = siteMapPio.ToString(CultureInfo.GetCultureInfo("en-US"));
                }
                else
                    node.SiteMapPriority = mojoParentNode.SiteMapPriority;
                node.UseSsl = mojoParentNode.UseSsl;
                node.MenuCssClass = mojoParentNode.MenuCssClass;
            }
            node.ParentNode = parentNode;

            node.IncludeInMenu = false;

            node.OpenInNewWindow = false;
            node.HideAfterLogin = false;
            node.IsPending = false;
            node.IsClickable = true;

            return node;
        }


    }
}